package cl.jpavlich.mpm.model;

import java.net.URL;
import java.util.LinkedHashSet;
import java.util.Set;

import org.metawidget.inspector.annotation.UiAction;

import cl.jpavlich.mpm.services.Serializer;
import de.schlichtherle.truezip.file.TFile;

public class ModRepository {
	private String name;
	private Set<ModPackage> packages = new LinkedHashSet<ModPackage>();
	
	public static ModRepository load(String url) {
		try {
			URL address = new URL(url);
			Serializer serializer = new Serializer();
			
			return serializer.deserialize(ModRepository.class, address.openStream());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public static void save(ModRepository r, String filename) {
		TFile f = new TFile(filename);
		
		Serializer s = new Serializer();
		s.serialize(r, f);
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<ModPackage> getPackages() {
		return packages;
	}

	@UiAction
	public void addPackage() {
		addPackage(new ModPackage());
	}
	
	public boolean addPackage(ModPackage pkg) {
		return packages.add(pkg);
	}
	
	public boolean removePackage(ModPackage pkg) {
		return packages.remove(pkg);
	}
}
